package com.yesep.learn.hik.utils;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图片工具类
 * Created by WangRupeng on 2017/8/16 0016.
 */
public class ImageUtils {
    static sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

    public static File saveToFile(BufferedImage bufferedImage, String path, String fileName, String imageType) throws IOException {
        if (bufferedImage == null || path == null) {
            return null;
        }
        if (!path.endsWith("/")) {
            path += "/";
        }
        File file = new File(path + fileName + "." + imageType);
        ImageIO.write(bufferedImage, imageType, file);
        return file;
    }

    /**
     * @param bufferedImage
     * @return
     * @author WangRupeng
     */
    public static byte[] decodeToPixels(BufferedImage bufferedImage) {
        if (bufferedImage == null)
            return null;
        return ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
    }

    public static String getImageBase64(File f, String type) throws IOException {
        BufferedImage bi = ImageIO.read(f);
        return getImageBase64(bi, type);
    }

    public static String getImageBase64(BufferedImage bi, String type) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(resizeBufferedImage(bi, 320, 220, true), type, baos);
        byte[] bytes = baos.toByteArray();
        return encoder.encode(bytes).trim();
    }

    /**
     * 改变bufferedImage 图片大小
     *
     * @param source
     * @param targetW
     * @param targetH
     * @param flag
     * @return
     */
    private static BufferedImage resizeBufferedImage(BufferedImage source, int targetW, int targetH, boolean flag) {
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) targetW / source.getWidth();
        double sy = (double) targetH / source.getHeight();
        if (flag && sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else if (flag && sx <= sy) {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
        if (type == BufferedImage.TYPE_CUSTOM) { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else {
            target = new BufferedImage(targetW, targetH, type);
        }
        Graphics2D g = target.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }
}
